//
//  NSDate+Util.m
//  Peanut
//
//  Created by Jim huang on 2018/3/12.
//  Copyright © 2018年 Jim_Peanut. All rights reserved.
//

#import "NSDate+Util.h"

@implementation NSDate (Util)

+(NSDate *)dateFromString:(NSString *)string withFormat:(NSString *)formatString
{
    NSDateFormatter* format = [[NSDateFormatter alloc] init];
    [format setDateFormat:formatString];
    NSDate* date = [format dateFromString:string];
    return date;
}

+(NSString *)stringFormDate:(NSDate *)date withFormat:(NSString *)formatString
{
    NSDateFormatter* format = [[NSDateFormatter alloc] init];
    [format setDateFormat:formatString];
    NSString* dateString = [format stringFromDate:date];
    return dateString;
}

+(NSInteger)daysFormDate:(NSDate *)formDate toDate:(NSDate *)toDate
{
    NSCalendar* chineseClendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    NSUInteger unitFlags = NSCalendarUnitDay;
    
    NSDateComponents *gaps = [chineseClendar components:unitFlags fromDate:formDate toDate:toDate  options:0];
    return [gaps day];
}


//时间戳转时间 @"yyyy-MM-dd hh:mm:ss"
+(NSString *)dateWithStrTimeInterval:(NSString *)StrTimeInterval fromatStr:(NSString *)fromatStr{
    NSTimeInterval interval = StrTimeInterval.integerValue / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:fromatStr];
    return [formatter stringFromDate: date];
}


@end
